In [1]:
import glob
import math
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
import numpy as np
import random
import sklearn.metrics as metrics

from tensorflow.keras import optimizers
from tensorflow.keras.callbacks import ModelCheckpoint, CSVLogger, LearningRateScheduler
from tensorflow.keras.models import Model
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.layers import add, concatenate, Conv2D, Dense, Dropout, Flatten, Input
from tensorflow.keras.layers import Activation, AveragePooling2D, BatchNormalization, MaxPooling2D
from tensorflow.keras.regularizers import l2
from tensorflow.keras.utils import to_categorical


%matplotlib inline
In [2]:
                            # Set up 'ggplot' style
plt.style.use('ggplot')     # if want to use the default style, set 'classic'
plt.rcParams['ytick.right']     = True
plt.rcParams['ytick.labelright']= True
plt.rcParams['ytick.left']      = False
plt.rcParams['ytick.labelleft'] = False
plt.rcParams['font.family']     = 'Arial'
In [3]:
# where am i?
%pwd
Out[3]:
'C:\\Users\\david\\Documents\\ImageNet'
In [4]:
flowers = glob.glob('./data/flr_*.jpg')
fungus = glob.glob('./data/fgs_*.jpg')
rocks = glob.glob('./data/rck_*.jpg')

pixel_flowers = glob.glob('./data/pxl_flower_*.jpeg')
pixel_umbrella = glob.glob('./data/pxl_umbrella_*.jpeg')
print("There are %s, %s flower, %s fungus, %s rock and %s umbrella pictures" %(len(flowers), len(pixel_flowers), len(fungus), len(rocks), len(pixel_umbrella)))
There are 1269, 1792 flower, 856 fungus, 1007 rock and 420 umbrella pictures
In [5]:
# Randomly show 10 examples of the images
from IPython.display import Image
    
dataset = flowers #flowers #fungus #rocks

for i in range(0, 5):
    index = random.randint(0, len(dataset)-1)   
    print("Showing:", dataset[index])
    
    img = mpimg.imread(dataset[index])
    imgplot = plt.imshow(img)
    plt.show()

#Image(dataset[index])
Showing: ./data\flr_01632.jpg
Showing: ./data\flr_01602.jpg
Showing: ./data\flr_01907.jpg
Showing: ./data\flr_01793.jpg
Showing: ./data\flr_00420.jpg

Extract the training and testing datasets

In [6]:
# Load the data
trDatOrg       = np.load('flrnonflr-train-imgs96-0.8.npz')['arr_0']
trLblOrg       = np.load('flrnonflr-train-labels96-0.8.npz')['arr_0']
tsDatOrg       = np.load('flrnonflr-test-imgs96-0.8.npz')['arr_0']
tsLblOrg       = np.load('flrnonflr-test-labels96-0.8.npz')['arr_0']
In [7]:
print("For the training and test datasets:")
print("The shapes are %s, %s, %s, %s" \
      %(trDatOrg.shape, trLblOrg.shape, tsDatOrg.shape, tsLblOrg.shape))
For the training and test datasets:
The shapes are (4264, 96, 96, 3), (4264,), (1067, 96, 96, 3), (1067,)
In [8]:
# Randomly show 10 examples of the images

data = tsDatOrg
label = tsLblOrg

for i in range(20):
    index = random.randint(0, len(data)-1)
    print("Showing %s index image, It is %s" %(index, label[index]))
    imgplot = plt.imshow(data[index])
    plt.show()
Showing 221 index image, It is 1.0
Showing 643 index image, It is 0.0
Showing 576 index image, It is 1.0
Showing 261 index image, It is 1.0
Showing 702 index image, It is 0.0
Showing 1043 index image, It is 0.0
Showing 643 index image, It is 0.0
Showing 33 index image, It is 1.0
Showing 505 index image, It is 1.0
Showing 962 index image, It is 0.0
Showing 1040 index image, It is 0.0
Showing 638 index image, It is 0.0
Showing 442 index image, It is 1.0
Showing 455 index image, It is 1.0
Showing 160 index image, It is 1.0
Showing 803 index image, It is 0.0
Showing 586 index image, It is 1.0
Showing 554 index image, It is 1.0
Showing 518 index image, It is 1.0
Showing 615 index image, It is 0.0
In [9]:
# Convert the data into 'float32'
# Rescale the values from 0~255 to 0~1
trDat       = trDatOrg.astype('float32')/255
tsDat       = tsDatOrg.astype('float32')/255

# Retrieve the row size of each image
# Retrieve the column size of each image
imgrows     = trDat.shape[1]
imgclms     = trDat.shape[2]
channel     = 3

# # reshape the data to be [samples][width][height][channel]
# # This is required by Keras framework
# trDat       = trDat.reshape(trDat.shape[0], imgrows, imgclms, channel)
# tsDat       = tsDat.reshape(tsDat.shape[0], imgrows, imgclms, channel)

# Perform one hot encoding on the labels
# Retrieve the number of classes in this problem
trLbl       = to_categorical(trLblOrg)
tsLbl       = to_categorical(tsLblOrg)
num_classes = tsLbl.shape[1]
In [10]:
# fix random seed for reproducibility
seed = 29
np.random.seed(seed)


modelname = 'FlowerPower'

def createBaselineModel():
    inputs = Input(shape=(imgrows, imgclms, channel))
    x = Conv2D(30, (4, 4), activation='relu')(inputs)
    x = MaxPooling2D(pool_size=(2, 2))(x)
    x = Conv2D(50, (4, 4), activation='relu')(x)
    x = MaxPooling2D(pool_size=(2, 2))(x)
    x = Dropout(0.3)(x)
    x = Flatten()(x)
    x = Dense(32, activation='relu')(x)
    x = Dense(num_classes, activation='softmax')(x)
    
    model = Model(inputs=[inputs],outputs=x)
    
    model.compile(loss='categorical_crossentropy', 
                  optimizer='adam',
                  metrics=['accuracy'])
    return model

optmz       = optimizers.Adam(lr=0.001)

def resLyr(inputs,
           numFilters=16,
           kernelSz=3,
           strides=1,
           activation='relu',
           batchNorm=True,
           convFirst=True,
           lyrName=None):
    convLyr = Conv2D(numFilters, kernel_size=kernelSz, strides=strides, 
                     padding='same', kernel_initializer='he_normal', 
                     kernel_regularizer=l2(1e-4), 
                     name=lyrName+'_conv' if lyrName else None)
    x = inputs
    if convFirst:
        x = convLyr(x)
        if batchNorm:
            x = BatchNormalization(name=lyrName+'_bn' if lyrName else None)(x)
        if activation is not None:
            x = Activation(activation,name=lyrName+'_'+activation if lyrName else None)(x)
    else:
        if batchNorm:
            x = BatchNormalization(name=lyrName+'_bn' if lyrName else None)(x)
        if activation is not None:
            x = Activation(activation, name=lyrName+'_'+activation if lyrName else None)(x)
        x = convLyr(x)
    return x


def resBlkV1(inputs,
             numFilters=16,
             numBlocks=3,
             downsampleOnFirst=True,
             names=None):
    x = inputs
    for run in range(0,numBlocks):
        strides = 1
        blkStr = str(run+1)
        if downsampleOnFirst and run == 0:
            strides = 2
        y = resLyr(inputs=x, numFilters=numFilters, strides=strides,
                   lyrName=names+'_Blk'+blkStr+'_Res1' if names else None)
        y = resLyr(inputs=y, numFilters=numFilters, activation=None,
                   lyrName=names+'_Blk'+blkStr+'_Res2' if names else None)
        if downsampleOnFirst and run == 0:
            x = resLyr(inputs=x, numFilters=numFilters, kernelSz=1,
                       strides=strides, activation=None, batchNorm=False,
                       lyrName=names+'_Blk'+blkStr+'_lin' if names else None)
        x = add([x,y], name=names+'_Blk'+blkStr+'_add' if names else None)
        x = Activation('relu', name=names+'_Blk'+blkStr+'_relu' if names else None)(x)
    return x

def createResNetV1(inputShape=(imgrows, imgclms, channel),
                   numClasses=2):
    inputs = Input(shape=inputShape)
    v = resLyr(inputs, lyrName='Inpt')
    v = resBlkV1(inputs=v, numFilters=16, numBlocks=3,
                 downsampleOnFirst=False, names='Stg1')
    v = Dropout(0.30)(v)
    v = resBlkV1(inputs=v, numFilters=32, numBlocks=3,
                 downsampleOnFirst=True, names='Stg2')
    v = Dropout(0.35)(v)
    v = resBlkV1(inputs=v, numFilters=64, numBlocks=3,
                 downsampleOnFirst=True, names='Stg3')
    v = Dropout(0.40)(v)
    v = resBlkV1(inputs=v, numFilters=128, numBlocks=3,
                 downsampleOnFirst=True, names='Stg4')
    v = Dropout(0.50)(v)
    v = AveragePooling2D(pool_size=8, name='AvgPool')(v)
    v = Flatten()(v) 
    outputs = Dense(numClasses, activation='softmax', 
                    kernel_initializer='he_normal')(v)
    model = Model(inputs=inputs,outputs=outputs)
    model.compile(loss='categorical_crossentropy', optimizer=optmz, 
                  metrics=['accuracy'])
    return model



# Setup the models
model       = createResNetV1() # This is meant for training
modelGo     = createResNetV1() # This is used for final testing

model.summary()
WARNING:tensorflow:From D:\DocumentsDDrive\Installed_Files\Anaconda3\envs\tf-gpu\lib\site-packages\tensorflow\python\keras\initializers.py:104: calling VarianceScaling.__init__ (from tensorflow.python.ops.init_ops) with distribution=normal is deprecated and will be removed in a future version.
Instructions for updating:
`normal` is a deprecated alias for `truncated_normal`
__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_1 (InputLayer)            (None, 96, 96, 3)    0                                            
__________________________________________________________________________________________________
Inpt_conv (Conv2D)              (None, 96, 96, 16)   448         input_1[0][0]                    
__________________________________________________________________________________________________
Inpt_bn (BatchNormalization)    (None, 96, 96, 16)   64          Inpt_conv[0][0]                  
__________________________________________________________________________________________________
Inpt_relu (Activation)          (None, 96, 96, 16)   0           Inpt_bn[0][0]                    
__________________________________________________________________________________________________
Stg1_Blk1_Res1_conv (Conv2D)    (None, 96, 96, 16)   2320        Inpt_relu[0][0]                  
__________________________________________________________________________________________________
Stg1_Blk1_Res1_bn (BatchNormali (None, 96, 96, 16)   64          Stg1_Blk1_Res1_conv[0][0]        
__________________________________________________________________________________________________
Stg1_Blk1_Res1_relu (Activation (None, 96, 96, 16)   0           Stg1_Blk1_Res1_bn[0][0]          
__________________________________________________________________________________________________
Stg1_Blk1_Res2_conv (Conv2D)    (None, 96, 96, 16)   2320        Stg1_Blk1_Res1_relu[0][0]        
__________________________________________________________________________________________________
Stg1_Blk1_Res2_bn (BatchNormali (None, 96, 96, 16)   64          Stg1_Blk1_Res2_conv[0][0]        
__________________________________________________________________________________________________
Stg1_Blk1_add (Add)             (None, 96, 96, 16)   0           Inpt_relu[0][0]                  
                                                                 Stg1_Blk1_Res2_bn[0][0]          
__________________________________________________________________________________________________
Stg1_Blk1_relu (Activation)     (None, 96, 96, 16)   0           Stg1_Blk1_add[0][0]              
__________________________________________________________________________________________________
Stg1_Blk2_Res1_conv (Conv2D)    (None, 96, 96, 16)   2320        Stg1_Blk1_relu[0][0]             
__________________________________________________________________________________________________
Stg1_Blk2_Res1_bn (BatchNormali (None, 96, 96, 16)   64          Stg1_Blk2_Res1_conv[0][0]        
__________________________________________________________________________________________________
Stg1_Blk2_Res1_relu (Activation (None, 96, 96, 16)   0           Stg1_Blk2_Res1_bn[0][0]          
__________________________________________________________________________________________________
Stg1_Blk2_Res2_conv (Conv2D)    (None, 96, 96, 16)   2320        Stg1_Blk2_Res1_relu[0][0]        
__________________________________________________________________________________________________
Stg1_Blk2_Res2_bn (BatchNormali (None, 96, 96, 16)   64          Stg1_Blk2_Res2_conv[0][0]        
__________________________________________________________________________________________________
Stg1_Blk2_add (Add)             (None, 96, 96, 16)   0           Stg1_Blk1_relu[0][0]             
                                                                 Stg1_Blk2_Res2_bn[0][0]          
__________________________________________________________________________________________________
Stg1_Blk2_relu (Activation)     (None, 96, 96, 16)   0           Stg1_Blk2_add[0][0]              
__________________________________________________________________________________________________
Stg1_Blk3_Res1_conv (Conv2D)    (None, 96, 96, 16)   2320        Stg1_Blk2_relu[0][0]             
__________________________________________________________________________________________________
Stg1_Blk3_Res1_bn (BatchNormali (None, 96, 96, 16)   64          Stg1_Blk3_Res1_conv[0][0]        
__________________________________________________________________________________________________
Stg1_Blk3_Res1_relu (Activation (None, 96, 96, 16)   0           Stg1_Blk3_Res1_bn[0][0]          
__________________________________________________________________________________________________
Stg1_Blk3_Res2_conv (Conv2D)    (None, 96, 96, 16)   2320        Stg1_Blk3_Res1_relu[0][0]        
__________________________________________________________________________________________________
Stg1_Blk3_Res2_bn (BatchNormali (None, 96, 96, 16)   64          Stg1_Blk3_Res2_conv[0][0]        
__________________________________________________________________________________________________
Stg1_Blk3_add (Add)             (None, 96, 96, 16)   0           Stg1_Blk2_relu[0][0]             
                                                                 Stg1_Blk3_Res2_bn[0][0]          
__________________________________________________________________________________________________
Stg1_Blk3_relu (Activation)     (None, 96, 96, 16)   0           Stg1_Blk3_add[0][0]              
__________________________________________________________________________________________________
dropout (Dropout)               (None, 96, 96, 16)   0           Stg1_Blk3_relu[0][0]             
__________________________________________________________________________________________________
Stg2_Blk1_Res1_conv (Conv2D)    (None, 48, 48, 32)   4640        dropout[0][0]                    
__________________________________________________________________________________________________
Stg2_Blk1_Res1_bn (BatchNormali (None, 48, 48, 32)   128         Stg2_Blk1_Res1_conv[0][0]        
__________________________________________________________________________________________________
Stg2_Blk1_Res1_relu (Activation (None, 48, 48, 32)   0           Stg2_Blk1_Res1_bn[0][0]          
__________________________________________________________________________________________________
Stg2_Blk1_Res2_conv (Conv2D)    (None, 48, 48, 32)   9248        Stg2_Blk1_Res1_relu[0][0]        
__________________________________________________________________________________________________
Stg2_Blk1_lin_conv (Conv2D)     (None, 48, 48, 32)   544         dropout[0][0]                    
__________________________________________________________________________________________________
Stg2_Blk1_Res2_bn (BatchNormali (None, 48, 48, 32)   128         Stg2_Blk1_Res2_conv[0][0]        
__________________________________________________________________________________________________
Stg2_Blk1_add (Add)             (None, 48, 48, 32)   0           Stg2_Blk1_lin_conv[0][0]         
                                                                 Stg2_Blk1_Res2_bn[0][0]          
__________________________________________________________________________________________________
Stg2_Blk1_relu (Activation)     (None, 48, 48, 32)   0           Stg2_Blk1_add[0][0]              
__________________________________________________________________________________________________
Stg2_Blk2_Res1_conv (Conv2D)    (None, 48, 48, 32)   9248        Stg2_Blk1_relu[0][0]             
__________________________________________________________________________________________________
Stg2_Blk2_Res1_bn (BatchNormali (None, 48, 48, 32)   128         Stg2_Blk2_Res1_conv[0][0]        
__________________________________________________________________________________________________
Stg2_Blk2_Res1_relu (Activation (None, 48, 48, 32)   0           Stg2_Blk2_Res1_bn[0][0]          
__________________________________________________________________________________________________
Stg2_Blk2_Res2_conv (Conv2D)    (None, 48, 48, 32)   9248        Stg2_Blk2_Res1_relu[0][0]        
__________________________________________________________________________________________________
Stg2_Blk2_Res2_bn (BatchNormali (None, 48, 48, 32)   128         Stg2_Blk2_Res2_conv[0][0]        
__________________________________________________________________________________________________
Stg2_Blk2_add (Add)             (None, 48, 48, 32)   0           Stg2_Blk1_relu[0][0]             
                                                                 Stg2_Blk2_Res2_bn[0][0]          
__________________________________________________________________________________________________
Stg2_Blk2_relu (Activation)     (None, 48, 48, 32)   0           Stg2_Blk2_add[0][0]              
__________________________________________________________________________________________________
Stg2_Blk3_Res1_conv (Conv2D)    (None, 48, 48, 32)   9248        Stg2_Blk2_relu[0][0]             
__________________________________________________________________________________________________
Stg2_Blk3_Res1_bn (BatchNormali (None, 48, 48, 32)   128         Stg2_Blk3_Res1_conv[0][0]        
__________________________________________________________________________________________________
Stg2_Blk3_Res1_relu (Activation (None, 48, 48, 32)   0           Stg2_Blk3_Res1_bn[0][0]          
__________________________________________________________________________________________________
Stg2_Blk3_Res2_conv (Conv2D)    (None, 48, 48, 32)   9248        Stg2_Blk3_Res1_relu[0][0]        
__________________________________________________________________________________________________
Stg2_Blk3_Res2_bn (BatchNormali (None, 48, 48, 32)   128         Stg2_Blk3_Res2_conv[0][0]        
__________________________________________________________________________________________________
Stg2_Blk3_add (Add)             (None, 48, 48, 32)   0           Stg2_Blk2_relu[0][0]             
                                                                 Stg2_Blk3_Res2_bn[0][0]          
__________________________________________________________________________________________________
Stg2_Blk3_relu (Activation)     (None, 48, 48, 32)   0           Stg2_Blk3_add[0][0]              
__________________________________________________________________________________________________
dropout_1 (Dropout)             (None, 48, 48, 32)   0           Stg2_Blk3_relu[0][0]             
__________________________________________________________________________________________________
Stg3_Blk1_Res1_conv (Conv2D)    (None, 24, 24, 64)   18496       dropout_1[0][0]                  
__________________________________________________________________________________________________
Stg3_Blk1_Res1_bn (BatchNormali (None, 24, 24, 64)   256         Stg3_Blk1_Res1_conv[0][0]        
__________________________________________________________________________________________________
Stg3_Blk1_Res1_relu (Activation (None, 24, 24, 64)   0           Stg3_Blk1_Res1_bn[0][0]          
__________________________________________________________________________________________________
Stg3_Blk1_Res2_conv (Conv2D)    (None, 24, 24, 64)   36928       Stg3_Blk1_Res1_relu[0][0]        
__________________________________________________________________________________________________
Stg3_Blk1_lin_conv (Conv2D)     (None, 24, 24, 64)   2112        dropout_1[0][0]                  
__________________________________________________________________________________________________
Stg3_Blk1_Res2_bn (BatchNormali (None, 24, 24, 64)   256         Stg3_Blk1_Res2_conv[0][0]        
__________________________________________________________________________________________________
Stg3_Blk1_add (Add)             (None, 24, 24, 64)   0           Stg3_Blk1_lin_conv[0][0]         
                                                                 Stg3_Blk1_Res2_bn[0][0]          
__________________________________________________________________________________________________
Stg3_Blk1_relu (Activation)     (None, 24, 24, 64)   0           Stg3_Blk1_add[0][0]              
__________________________________________________________________________________________________
Stg3_Blk2_Res1_conv (Conv2D)    (None, 24, 24, 64)   36928       Stg3_Blk1_relu[0][0]             
__________________________________________________________________________________________________
Stg3_Blk2_Res1_bn (BatchNormali (None, 24, 24, 64)   256         Stg3_Blk2_Res1_conv[0][0]        
__________________________________________________________________________________________________
Stg3_Blk2_Res1_relu (Activation (None, 24, 24, 64)   0           Stg3_Blk2_Res1_bn[0][0]          
__________________________________________________________________________________________________
Stg3_Blk2_Res2_conv (Conv2D)    (None, 24, 24, 64)   36928       Stg3_Blk2_Res1_relu[0][0]        
__________________________________________________________________________________________________
Stg3_Blk2_Res2_bn (BatchNormali (None, 24, 24, 64)   256         Stg3_Blk2_Res2_conv[0][0]        
__________________________________________________________________________________________________
Stg3_Blk2_add (Add)             (None, 24, 24, 64)   0           Stg3_Blk1_relu[0][0]             
                                                                 Stg3_Blk2_Res2_bn[0][0]          
__________________________________________________________________________________________________
Stg3_Blk2_relu (Activation)     (None, 24, 24, 64)   0           Stg3_Blk2_add[0][0]              
__________________________________________________________________________________________________
Stg3_Blk3_Res1_conv (Conv2D)    (None, 24, 24, 64)   36928       Stg3_Blk2_relu[0][0]             
__________________________________________________________________________________________________
Stg3_Blk3_Res1_bn (BatchNormali (None, 24, 24, 64)   256         Stg3_Blk3_Res1_conv[0][0]        
__________________________________________________________________________________________________
Stg3_Blk3_Res1_relu (Activation (None, 24, 24, 64)   0           Stg3_Blk3_Res1_bn[0][0]          
__________________________________________________________________________________________________
Stg3_Blk3_Res2_conv (Conv2D)    (None, 24, 24, 64)   36928       Stg3_Blk3_Res1_relu[0][0]        
__________________________________________________________________________________________________
Stg3_Blk3_Res2_bn (BatchNormali (None, 24, 24, 64)   256         Stg3_Blk3_Res2_conv[0][0]        
__________________________________________________________________________________________________
Stg3_Blk3_add (Add)             (None, 24, 24, 64)   0           Stg3_Blk2_relu[0][0]             
                                                                 Stg3_Blk3_Res2_bn[0][0]          
__________________________________________________________________________________________________
Stg3_Blk3_relu (Activation)     (None, 24, 24, 64)   0           Stg3_Blk3_add[0][0]              
__________________________________________________________________________________________________
dropout_2 (Dropout)             (None, 24, 24, 64)   0           Stg3_Blk3_relu[0][0]             
__________________________________________________________________________________________________
Stg4_Blk1_Res1_conv (Conv2D)    (None, 12, 12, 128)  73856       dropout_2[0][0]                  
__________________________________________________________________________________________________
Stg4_Blk1_Res1_bn (BatchNormali (None, 12, 12, 128)  512         Stg4_Blk1_Res1_conv[0][0]        
__________________________________________________________________________________________________
Stg4_Blk1_Res1_relu (Activation (None, 12, 12, 128)  0           Stg4_Blk1_Res1_bn[0][0]          
__________________________________________________________________________________________________
Stg4_Blk1_Res2_conv (Conv2D)    (None, 12, 12, 128)  147584      Stg4_Blk1_Res1_relu[0][0]        
__________________________________________________________________________________________________
Stg4_Blk1_lin_conv (Conv2D)     (None, 12, 12, 128)  8320        dropout_2[0][0]                  
__________________________________________________________________________________________________
Stg4_Blk1_Res2_bn (BatchNormali (None, 12, 12, 128)  512         Stg4_Blk1_Res2_conv[0][0]        
__________________________________________________________________________________________________
Stg4_Blk1_add (Add)             (None, 12, 12, 128)  0           Stg4_Blk1_lin_conv[0][0]         
                                                                 Stg4_Blk1_Res2_bn[0][0]          
__________________________________________________________________________________________________
Stg4_Blk1_relu (Activation)     (None, 12, 12, 128)  0           Stg4_Blk1_add[0][0]              
__________________________________________________________________________________________________
Stg4_Blk2_Res1_conv (Conv2D)    (None, 12, 12, 128)  147584      Stg4_Blk1_relu[0][0]             
__________________________________________________________________________________________________
Stg4_Blk2_Res1_bn (BatchNormali (None, 12, 12, 128)  512         Stg4_Blk2_Res1_conv[0][0]        
__________________________________________________________________________________________________
Stg4_Blk2_Res1_relu (Activation (None, 12, 12, 128)  0           Stg4_Blk2_Res1_bn[0][0]          
__________________________________________________________________________________________________
Stg4_Blk2_Res2_conv (Conv2D)    (None, 12, 12, 128)  147584      Stg4_Blk2_Res1_relu[0][0]        
__________________________________________________________________________________________________
Stg4_Blk2_Res2_bn (BatchNormali (None, 12, 12, 128)  512         Stg4_Blk2_Res2_conv[0][0]        
__________________________________________________________________________________________________
Stg4_Blk2_add (Add)             (None, 12, 12, 128)  0           Stg4_Blk1_relu[0][0]             
                                                                 Stg4_Blk2_Res2_bn[0][0]          
__________________________________________________________________________________________________
Stg4_Blk2_relu (Activation)     (None, 12, 12, 128)  0           Stg4_Blk2_add[0][0]              
__________________________________________________________________________________________________
Stg4_Blk3_Res1_conv (Conv2D)    (None, 12, 12, 128)  147584      Stg4_Blk2_relu[0][0]             
__________________________________________________________________________________________________
Stg4_Blk3_Res1_bn (BatchNormali (None, 12, 12, 128)  512         Stg4_Blk3_Res1_conv[0][0]        
__________________________________________________________________________________________________
Stg4_Blk3_Res1_relu (Activation (None, 12, 12, 128)  0           Stg4_Blk3_Res1_bn[0][0]          
__________________________________________________________________________________________________
Stg4_Blk3_Res2_conv (Conv2D)    (None, 12, 12, 128)  147584      Stg4_Blk3_Res1_relu[0][0]        
__________________________________________________________________________________________________
Stg4_Blk3_Res2_bn (BatchNormali (None, 12, 12, 128)  512         Stg4_Blk3_Res2_conv[0][0]        
__________________________________________________________________________________________________
Stg4_Blk3_add (Add)             (None, 12, 12, 128)  0           Stg4_Blk2_relu[0][0]             
                                                                 Stg4_Blk3_Res2_bn[0][0]          
__________________________________________________________________________________________________
Stg4_Blk3_relu (Activation)     (None, 12, 12, 128)  0           Stg4_Blk3_add[0][0]              
__________________________________________________________________________________________________
dropout_3 (Dropout)             (None, 12, 12, 128)  0           Stg4_Blk3_relu[0][0]             
__________________________________________________________________________________________________
AvgPool (AveragePooling2D)      (None, 1, 1, 128)    0           dropout_3[0][0]                  
__________________________________________________________________________________________________
flatten (Flatten)               (None, 128)          0           AvgPool[0][0]                    
__________________________________________________________________________________________________
dense (Dense)                   (None, 2)            258         flatten[0][0]                    
==================================================================================================
Total params: 1,097,218
Trainable params: 1,094,306
Non-trainable params: 2,912
__________________________________________________________________________________________________
In [11]:
# Create checkpoint for the training
# This checkpoint performs model saving when
# an epoch gives highest testing accuracy
# filepath        = modelname + ".hdf5"
# checkpoint      = ModelCheckpoint(filepath, 
#                                   monitor='val_acc', 
#                                   verbose=0, 
#                                   save_best_only=True, 
#                                   mode='max')

#                             # Log the epoch detail into csv
# csv_logger      = CSVLogger(modelname +'.csv')
# callbacks_list  = [checkpoint,csv_logger]

def lrSchedule(epoch):
    lr  = 1e-3
    
    if epoch > 70:
        lr  *= 0.5e-3
        
    elif epoch > 50:
        lr  *= 1e-3
        
    elif epoch > 40:
        lr  *= 1e-2
        
    elif epoch > 30:
        lr  *= 1e-1
        
    print('Learning rate: ', lr)
    
    return lr

LRScheduler     = LearningRateScheduler(lrSchedule)

                            # Create checkpoint for the training
                            # This checkpoint performs model saving when
                            # an epoch gives highest testing accuracy
filepath        = modelname + ".hdf5"
checkpoint      = ModelCheckpoint(filepath, 
                                  monitor='val_acc', 
                                  verbose=0, 
                                  save_best_only=True, 
                                  mode='max')

                            # Log the epoch detail into csv
csv_logger      = CSVLogger(modelname +'.csv')
callbacks_list  = [checkpoint, csv_logger, LRScheduler]
#callbacks_list  = [checkpoint, csv_logger]
In [12]:
# Fit the model
# This is where the training starts
# model.fit(trDat, 
#           trLbl, 
#           validation_data=(tsDat, tsLbl), 
#           epochs=120, 
#           batch_size=32,
#           callbacks=callbacks_list)

datagen = ImageDataGenerator(width_shift_range=0.1,
                             height_shift_range=0.1,
                             rotation_range=30,
                             horizontal_flip=True,
                             vertical_flip=False)

model.fit_generator(datagen.flow(trDat, trLbl, batch_size=32),
                    validation_data=(tsDat, tsLbl),
                    epochs=120, 
                    verbose=1,
                    steps_per_epoch=len(trDat)/32,
                    callbacks=callbacks_list)
Learning rate:  0.001
Epoch 1/120
134/133 [==============================] - 45s 336ms/step - loss: 1.0017 - acc: 0.6973 - val_loss: 1.3851 - val_acc: 0.4845
Learning rate:  0.001
Epoch 2/120
134/133 [==============================] - 29s 213ms/step - loss: 0.8283 - acc: 0.7798 - val_loss: 1.2368 - val_acc: 0.6270
Learning rate:  0.001
Epoch 3/120
134/133 [==============================] - 29s 213ms/step - loss: 0.7829 - acc: 0.7915 - val_loss: 0.6970 - val_acc: 0.8322
Learning rate:  0.001
Epoch 4/120
134/133 [==============================] - 28s 212ms/step - loss: 0.7392 - acc: 0.8041 - val_loss: 0.7067 - val_acc: 0.8172
Learning rate:  0.001
Epoch 5/120
134/133 [==============================] - 28s 212ms/step - loss: 0.7021 - acc: 0.8237 - val_loss: 0.6566 - val_acc: 0.8294
Learning rate:  0.001
Epoch 6/120
134/133 [==============================] - 29s 214ms/step - loss: 0.6659 - acc: 0.8218 - val_loss: 0.8273 - val_acc: 0.7479
Learning rate:  0.001
Epoch 7/120
134/133 [==============================] - 29s 214ms/step - loss: 0.6482 - acc: 0.8298 - val_loss: 0.6136 - val_acc: 0.8351
Learning rate:  0.001
Epoch 8/120
134/133 [==============================] - 29s 214ms/step - loss: 0.6131 - acc: 0.8312 - val_loss: 0.6196 - val_acc: 0.8388
Learning rate:  0.001
Epoch 9/120
134/133 [==============================] - 28s 212ms/step - loss: 0.5885 - acc: 0.8405 - val_loss: 0.7255 - val_acc: 0.7470oss:
Learning rate:  0.001
Epoch 10/120
134/133 [==============================] - 28s 212ms/step - loss: 0.5649 - acc: 0.8482 - val_loss: 0.6137 - val_acc: 0.8257
Learning rate:  0.001
Epoch 11/120
134/133 [==============================] - 29s 217ms/step - loss: 0.5373 - acc: 0.8603 - val_loss: 0.5675 - val_acc: 0.8397
Learning rate:  0.001
Epoch 12/120
134/133 [==============================] - 29s 214ms/step - loss: 0.5154 - acc: 0.8652 - val_loss: 0.4871 - val_acc: 0.8810
Learning rate:  0.001
Epoch 13/120
134/133 [==============================] - 28s 212ms/step - loss: 0.5056 - acc: 0.8675 - val_loss: 0.4901 - val_acc: 0.8763
Learning rate:  0.001
Epoch 14/120
134/133 [==============================] - 29s 213ms/step - loss: 0.4825 - acc: 0.8652 - val_loss: 0.4533 - val_acc: 0.8847
Learning rate:  0.001
Epoch 15/120
134/133 [==============================] - 28s 211ms/step - loss: 0.4537 - acc: 0.8780 - val_loss: 0.6292 - val_acc: 0.8051
Learning rate:  0.001
Epoch 16/120
134/133 [==============================] - 28s 211ms/step - loss: 0.4508 - acc: 0.8766 - val_loss: 0.4908 - val_acc: 0.8716
Learning rate:  0.001
Epoch 17/120
134/133 [==============================] - 28s 211ms/step - loss: 0.4339 - acc: 0.8846 - val_loss: 0.4741 - val_acc: 0.8754
Learning rate:  0.001
Epoch 18/120
134/133 [==============================] - 28s 211ms/step - loss: 0.4303 - acc: 0.8792 - val_loss: 0.4604 - val_acc: 0.8641
Learning rate:  0.001
Epoch 19/120
134/133 [==============================] - 28s 211ms/step - loss: 0.4262 - acc: 0.8850 - val_loss: 0.3957 - val_acc: 0.8782
Learning rate:  0.001
Epoch 20/120
134/133 [==============================] - 28s 212ms/step - loss: 0.4089 - acc: 0.8864 - val_loss: 0.3894 - val_acc: 0.9035
Learning rate:  0.001
Epoch 21/120
134/133 [==============================] - 28s 211ms/step - loss: 0.3868 - acc: 0.8916 - val_loss: 0.4395 - val_acc: 0.8622
Learning rate:  0.001
Epoch 22/120
134/133 [==============================] - 28s 211ms/step - loss: 0.4079 - acc: 0.8827 - val_loss: 0.3938 - val_acc: 0.8782
Learning rate:  0.001
Epoch 23/120
134/133 [==============================] - 28s 211ms/step - loss: 0.3699 - acc: 0.8899 - val_loss: 0.3885 - val_acc: 0.8922
Learning rate:  0.001
Epoch 24/120
134/133 [==============================] - 28s 211ms/step - loss: 0.3704 - acc: 0.8937 - val_loss: 0.3724 - val_acc: 0.8969
Learning rate:  0.001
Epoch 25/120
134/133 [==============================] - 28s 211ms/step - loss: 0.3581 - acc: 0.8979 - val_loss: 0.4939 - val_acc: 0.8313
Learning rate:  0.001
Epoch 26/120
134/133 [==============================] - 28s 211ms/step - loss: 0.3565 - acc: 0.8962 - val_loss: 0.3672 - val_acc: 0.8950
Learning rate:  0.001
Epoch 27/120
134/133 [==============================] - 28s 211ms/step - loss: 0.3517 - acc: 0.8990 - val_loss: 0.3682 - val_acc: 0.8875
Learning rate:  0.001
Epoch 28/120
134/133 [==============================] - 29s 214ms/step - loss: 0.3541 - acc: 0.8948 - val_loss: 0.4335 - val_acc: 0.8510
Learning rate:  0.001
Epoch 29/120
134/133 [==============================] - 28s 211ms/step - loss: 0.3467 - acc: 0.8974 - val_loss: 0.5092 - val_acc: 0.8351
Learning rate:  0.001
Epoch 30/120
134/133 [==============================] - 29s 214ms/step - loss: 0.3419 - acc: 0.8990 - val_loss: 0.5089 - val_acc: 0.7919
Learning rate:  0.001
Epoch 31/120
134/133 [==============================] - 28s 211ms/step - loss: 0.3338 - acc: 0.9025 - val_loss: 0.7223 - val_acc: 0.7132
Learning rate:  0.0001
Epoch 32/120
134/133 [==============================] - 28s 211ms/step - loss: 0.3081 - acc: 0.9081 - val_loss: 0.3367 - val_acc: 0.9016
Learning rate:  0.0001
Epoch 33/120
134/133 [==============================] - 28s 211ms/step - loss: 0.2917 - acc: 0.9177 - val_loss: 0.3317 - val_acc: 0.9007
Learning rate:  0.0001
Epoch 34/120
134/133 [==============================] - 29s 214ms/step - loss: 0.2919 - acc: 0.9172 - val_loss: 0.3430 - val_acc: 0.8922
Learning rate:  0.0001
Epoch 35/120
134/133 [==============================] - 28s 211ms/step - loss: 0.2947 - acc: 0.9149 - val_loss: 0.3245 - val_acc: 0.9016
Learning rate:  0.0001
Epoch 36/120
134/133 [==============================] - 29s 213ms/step - loss: 0.2847 - acc: 0.9188 - val_loss: 0.3150 - val_acc: 0.9072
Learning rate:  0.0001
Epoch 37/120
134/133 [==============================] - 28s 211ms/step - loss: 0.2781 - acc: 0.9212 - val_loss: 0.3585 - val_acc: 0.8828
Learning rate:  0.0001
Epoch 38/120
134/133 [==============================] - 29s 214ms/step - loss: 0.2836 - acc: 0.9221 - val_loss: 0.3236 - val_acc: 0.9035
Learning rate:  0.0001
Epoch 39/120
134/133 [==============================] - 28s 211ms/step - loss: 0.2723 - acc: 0.9272 - val_loss: 0.3340 - val_acc: 0.8903
Learning rate:  0.0001
Epoch 40/120
134/133 [==============================] - 28s 213ms/step - loss: 0.2743 - acc: 0.9209 - val_loss: 0.3159 - val_acc: 0.9072
Learning rate:  0.0001
Epoch 41/120
134/133 [==============================] - 28s 211ms/step - loss: 0.2741 - acc: 0.9191 - val_loss: 0.4139 - val_acc: 0.8632
Learning rate:  1e-05
Epoch 42/120
134/133 [==============================] - 28s 211ms/step - loss: 0.2674 - acc: 0.9181 - val_loss: 0.3421 - val_acc: 0.8960
Learning rate:  1e-05
Epoch 43/120
134/133 [==============================] - 28s 211ms/step - loss: 0.2683 - acc: 0.9230 - val_loss: 0.3259 - val_acc: 0.9007
Learning rate:  1e-05
Epoch 44/120
134/133 [==============================] - 28s 211ms/step - loss: 0.2719 - acc: 0.9268 - val_loss: 0.3264 - val_acc: 0.8988
Learning rate:  1e-05
Epoch 45/120
134/133 [==============================] - 28s 211ms/step - loss: 0.2656 - acc: 0.9272 - val_loss: 0.3294 - val_acc: 0.8960
Learning rate:  1e-05
Epoch 46/120
134/133 [==============================] - 29s 215ms/step - loss: 0.2610 - acc: 0.9300 - val_loss: 0.3260 - val_acc: 0.8997
Learning rate:  1e-05
Epoch 47/120
134/133 [==============================] - 28s 211ms/step - loss: 0.2698 - acc: 0.9233 - val_loss: 0.3275 - val_acc: 0.8997
Learning rate:  1e-05
Epoch 48/120
134/133 [==============================] - 29s 213ms/step - loss: 0.2648 - acc: 0.9265 - val_loss: 0.3299 - val_acc: 0.8978
Learning rate:  1e-05
Epoch 49/120
134/133 [==============================] - 29s 214ms/step - loss: 0.2566 - acc: 0.9291 - val_loss: 0.3273 - val_acc: 0.9016
Learning rate:  1e-05
Epoch 50/120
134/133 [==============================] - 29s 215ms/step - loss: 0.2643 - acc: 0.9291 - val_loss: 0.3297 - val_acc: 0.8988
Learning rate:  1e-05
Epoch 51/120
134/133 [==============================] - 29s 214ms/step - loss: 0.2621 - acc: 0.9258 - val_loss: 0.3279 - val_acc: 0.8997
Learning rate:  1e-06
Epoch 52/120
134/133 [==============================] - 28s 211ms/step - loss: 0.2631 - acc: 0.9270 - val_loss: 0.3309 - val_acc: 0.8978
Learning rate:  1e-06
Epoch 53/120
134/133 [==============================] - 29s 216ms/step - loss: 0.2615 - acc: 0.9228 - val_loss: 0.3314 - val_acc: 0.8978
Learning rate:  1e-06
Epoch 54/120
134/133 [==============================] - 29s 214ms/step - loss: 0.2695 - acc: 0.9263 - val_loss: 0.3330 - val_acc: 0.8960
Learning rate:  1e-06
Epoch 55/120
134/133 [==============================] - 28s 212ms/step - loss: 0.2644 - acc: 0.9247 - val_loss: 0.3316 - val_acc: 0.8988
Learning rate:  1e-06
Epoch 56/120
134/133 [==============================] - 28s 212ms/step - loss: 0.2598 - acc: 0.9300 - val_loss: 0.3321 - val_acc: 0.8978
Learning rate:  1e-06
Epoch 57/120
134/133 [==============================] - 28s 212ms/step - loss: 0.2548 - acc: 0.9291 - val_loss: 0.3315 - val_acc: 0.8978s: 0.2556 - a
Learning rate:  1e-06
Epoch 58/120
134/133 [==============================] - 29s 215ms/step - loss: 0.2623 - acc: 0.9263 - val_loss: 0.3321 - val_acc: 0.8978
Learning rate:  1e-06
Epoch 59/120
134/133 [==============================] - 28s 212ms/step - loss: 0.2665 - acc: 0.9270 - val_loss: 0.3338 - val_acc: 0.8960
Learning rate:  1e-06
Epoch 60/120
134/133 [==============================] - 28s 211ms/step - loss: 0.2694 - acc: 0.9233 - val_loss: 0.3331 - val_acc: 0.8960
Learning rate:  1e-06
Epoch 61/120
134/133 [==============================] - 29s 215ms/step - loss: 0.2672 - acc: 0.9251 - val_loss: 0.3314 - val_acc: 0.8978
Learning rate:  1e-06
Epoch 62/120
134/133 [==============================] - 28s 212ms/step - loss: 0.2615 - acc: 0.9261 - val_loss: 0.3315 - val_acc: 0.8988
Learning rate:  1e-06
Epoch 63/120
134/133 [==============================] - 28s 211ms/step - loss: 0.2642 - acc: 0.9242 - val_loss: 0.3314 - val_acc: 0.8988
Learning rate:  1e-06
Epoch 64/120
134/133 [==============================] - 28s 212ms/step - loss: 0.2576 - acc: 0.9265 - val_loss: 0.3315 - val_acc: 0.8988
Learning rate:  1e-06
Epoch 65/120
134/133 [==============================] - 29s 218ms/step - loss: 0.2561 - acc: 0.9272 - val_loss: 0.3301 - val_acc: 0.8988
Learning rate:  1e-06
Epoch 66/120
134/133 [==============================] - 29s 214ms/step - loss: 0.2574 - acc: 0.9270 - val_loss: 0.3297 - val_acc: 0.8988
Learning rate:  1e-06
Epoch 67/120
134/133 [==============================] - 28s 212ms/step - loss: 0.2599 - acc: 0.9284 - val_loss: 0.3309 - val_acc: 0.8988
Learning rate:  1e-06
Epoch 68/120
134/133 [==============================] - 28s 212ms/step - loss: 0.2574 - acc: 0.9300 - val_loss: 0.3310 - val_acc: 0.8988
Learning rate:  1e-06
Epoch 69/120
134/133 [==============================] - 28s 211ms/step - loss: 0.2603 - acc: 0.9284 - val_loss: 0.3315 - val_acc: 0.8988
Learning rate:  1e-06
Epoch 70/120
134/133 [==============================] - 29s 214ms/step - loss: 0.2633 - acc: 0.9303 - val_loss: 0.3313 - val_acc: 0.8988
Learning rate:  1e-06
Epoch 71/120
134/133 [==============================] - 28s 211ms/step - loss: 0.2596 - acc: 0.9277 - val_loss: 0.3308 - val_acc: 0.8988
Learning rate:  5e-07
Epoch 72/120
134/133 [==============================] - 28s 211ms/step - loss: 0.2568 - acc: 0.9303 - val_loss: 0.3307 - val_acc: 0.8988
Learning rate:  5e-07
Epoch 73/120
134/133 [==============================] - 28s 211ms/step - loss: 0.2613 - acc: 0.9293 - val_loss: 0.3315 - val_acc: 0.8988
Learning rate:  5e-07
Epoch 74/120
134/133 [==============================] - 29s 215ms/step - loss: 0.2690 - acc: 0.9247 - val_loss: 0.3308 - val_acc: 0.8988
Learning rate:  5e-07
Epoch 75/120
134/133 [==============================] - 28s 211ms/step - loss: 0.2545 - acc: 0.9317 - val_loss: 0.3315 - val_acc: 0.8988
Learning rate:  5e-07
Epoch 76/120
134/133 [==============================] - 28s 211ms/step - loss: 0.2543 - acc: 0.9312 - val_loss: 0.3312 - val_acc: 0.8988
Learning rate:  5e-07
Epoch 77/120
134/133 [==============================] - 29s 214ms/step - loss: 0.2644 - acc: 0.9291 - val_loss: 0.3316 - val_acc: 0.8988
Learning rate:  5e-07
Epoch 78/120
134/133 [==============================] - 28s 211ms/step - loss: 0.2562 - acc: 0.9282 - val_loss: 0.3319 - val_acc: 0.8997
Learning rate:  5e-07
Epoch 79/120
134/133 [==============================] - 28s 211ms/step - loss: 0.2619 - acc: 0.9254 - val_loss: 0.3304 - val_acc: 0.8988
Learning rate:  5e-07
Epoch 80/120
134/133 [==============================] - 29s 214ms/step - loss: 0.2643 - acc: 0.9256 - val_loss: 0.3312 - val_acc: 0.8988
Learning rate:  5e-07
Epoch 81/120
134/133 [==============================] - 28s 211ms/step - loss: 0.2590 - acc: 0.9230 - val_loss: 0.3298 - val_acc: 0.8988
Learning rate:  5e-07
Epoch 82/120
134/133 [==============================] - 28s 212ms/step - loss: 0.2543 - acc: 0.9307 - val_loss: 0.3306 - val_acc: 0.8988
Learning rate:  5e-07
Epoch 83/120
134/133 [==============================] - 29s 218ms/step - loss: 0.2582 - acc: 0.9307 - val_loss: 0.3313 - val_acc: 0.8988
Learning rate:  5e-07
Epoch 84/120
134/133 [==============================] - 28s 211ms/step - loss: 0.2556 - acc: 0.9305 - val_loss: 0.3312 - val_acc: 0.8988
Learning rate:  5e-07
Epoch 85/120
134/133 [==============================] - 28s 212ms/step - loss: 0.2513 - acc: 0.9333 - val_loss: 0.3302 - val_acc: 0.8997- loss: 0.2
Learning rate:  5e-07
Epoch 86/120
134/133 [==============================] - 29s 215ms/step - loss: 0.2491 - acc: 0.9291 - val_loss: 0.3320 - val_acc: 0.8978
Learning rate:  5e-07
Epoch 87/120
134/133 [==============================] - 28s 212ms/step - loss: 0.2633 - acc: 0.9289 - val_loss: 0.3308 - val_acc: 0.8988
Learning rate:  5e-07
Epoch 88/120
134/133 [==============================] - 29s 214ms/step - loss: 0.2547 - acc: 0.9289 - val_loss: 0.3305 - val_acc: 0.8988
Learning rate:  5e-07
Epoch 89/120
134/133 [==============================] - 29s 214ms/step - loss: 0.2627 - acc: 0.9263 - val_loss: 0.3313 - val_acc: 0.8988
Learning rate:  5e-07
Epoch 90/120
134/133 [==============================] - 28s 212ms/step - loss: 0.2556 - acc: 0.9293 - val_loss: 0.3311 - val_acc: 0.8988
Learning rate:  5e-07
Epoch 91/120
134/133 [==============================] - 29s 214ms/step - loss: 0.2567 - acc: 0.9282 - val_loss: 0.3313 - val_acc: 0.8988
Learning rate:  5e-07
Epoch 92/120
134/133 [==============================] - 29s 218ms/step - loss: 0.2552 - acc: 0.9282 - val_loss: 0.3319 - val_acc: 0.8997
Learning rate:  5e-07
Epoch 93/120
134/133 [==============================] - 28s 212ms/step - loss: 0.2556 - acc: 0.9307 - val_loss: 0.3306 - val_acc: 0.8988
Learning rate:  5e-07
Epoch 94/120
134/133 [==============================] - 28s 211ms/step - loss: 0.2608 - acc: 0.9307 - val_loss: 0.3306 - val_acc: 0.8988
Learning rate:  5e-07
Epoch 95/120
134/133 [==============================] - 28s 211ms/step - loss: 0.2607 - acc: 0.9275 - val_loss: 0.3316 - val_acc: 0.8988
Learning rate:  5e-07
Epoch 96/120
134/133 [==============================] - 28s 211ms/step - loss: 0.2557 - acc: 0.9293 - val_loss: 0.3318 - val_acc: 0.8997
Learning rate:  5e-07
Epoch 97/120
134/133 [==============================] - 28s 212ms/step - loss: 0.2590 - acc: 0.9279 - val_loss: 0.3315 - val_acc: 0.8988
Learning rate:  5e-07
Epoch 98/120
134/133 [==============================] - 28s 212ms/step - loss: 0.2645 - acc: 0.9305 - val_loss: 0.3328 - val_acc: 0.8988
Learning rate:  5e-07
Epoch 99/120
134/133 [==============================] - 29s 214ms/step - loss: 0.2599 - acc: 0.9282 - val_loss: 0.3315 - val_acc: 0.8978
Learning rate:  5e-07
Epoch 100/120
134/133 [==============================] - 28s 211ms/step - loss: 0.2551 - acc: 0.9265 - val_loss: 0.3302 - val_acc: 0.8988
Learning rate:  5e-07
Epoch 101/120
134/133 [==============================] - 29s 214ms/step - loss: 0.2642 - acc: 0.9268 - val_loss: 0.3307 - val_acc: 0.8988
Learning rate:  5e-07
Epoch 102/120
134/133 [==============================] - 28s 212ms/step - loss: 0.2560 - acc: 0.9324 - val_loss: 0.3301 - val_acc: 0.8988
Learning rate:  5e-07
Epoch 103/120
134/133 [==============================] - 29s 214ms/step - loss: 0.2611 - acc: 0.9247 - val_loss: 0.3315 - val_acc: 0.8997
Learning rate:  5e-07
Epoch 104/120
134/133 [==============================] - 28s 212ms/step - loss: 0.2588 - acc: 0.9256 - val_loss: 0.3310 - val_acc: 0.8988
Learning rate:  5e-07
Epoch 105/120
134/133 [==============================] - 28s 212ms/step - loss: 0.2532 - acc: 0.9291 - val_loss: 0.3315 - val_acc: 0.8988
Learning rate:  5e-07
Epoch 106/120
134/133 [==============================] - 28s 212ms/step - loss: 0.2564 - acc: 0.9277 - val_loss: 0.3313 - val_acc: 0.8978
Learning rate:  5e-07
Epoch 107/120
134/133 [==============================] - 29s 214ms/step - loss: 0.2630 - acc: 0.9254 - val_loss: 0.3316 - val_acc: 0.8988
Learning rate:  5e-07
Epoch 108/120
134/133 [==============================] - 28s 212ms/step - loss: 0.2585 - acc: 0.9249 - val_loss: 0.3314 - val_acc: 0.8988
Learning rate:  5e-07
Epoch 109/120
134/133 [==============================] - 28s 212ms/step - loss: 0.2569 - acc: 0.9333 - val_loss: 0.3316 - val_acc: 0.8988
Learning rate:  5e-07
Epoch 110/120
134/133 [==============================] - 29s 215ms/step - loss: 0.2501 - acc: 0.9338 - val_loss: 0.3330 - val_acc: 0.8978
Learning rate:  5e-07
Epoch 111/120
134/133 [==============================] - 28s 211ms/step - loss: 0.2509 - acc: 0.9314 - val_loss: 0.3324 - val_acc: 0.8988
Learning rate:  5e-07
Epoch 112/120
134/133 [==============================] - 28s 212ms/step - loss: 0.2572 - acc: 0.9305 - val_loss: 0.3324 - val_acc: 0.8978
Learning rate:  5e-07
Epoch 113/120
134/133 [==============================] - 28s 212ms/step - loss: 0.2609 - acc: 0.9254 - val_loss: 0.3331 - val_acc: 0.8978
Learning rate:  5e-07
Epoch 114/120
134/133 [==============================] - 28s 212ms/step - loss: 0.2621 - acc: 0.9272 - val_loss: 0.3316 - val_acc: 0.8988
Learning rate:  5e-07
Epoch 115/120
134/133 [==============================] - 28s 212ms/step - loss: 0.2526 - acc: 0.9310 - val_loss: 0.3312 - val_acc: 0.8988
Learning rate:  5e-07
Epoch 116/120
134/133 [==============================] - 29s 215ms/step - loss: 0.2507 - acc: 0.9319 - val_loss: 0.3313 - val_acc: 0.8988
Learning rate:  5e-07
Epoch 117/120
134/133 [==============================] - 28s 212ms/step - loss: 0.2636 - acc: 0.9251 - val_loss: 0.3313 - val_acc: 0.8988
Learning rate:  5e-07
Epoch 118/120
134/133 [==============================] - 28s 212ms/step - loss: 0.2560 - acc: 0.9307 - val_loss: 0.3315 - val_acc: 0.8978
Learning rate:  5e-07
Epoch 119/120
134/133 [==============================] - 29s 214ms/step - loss: 0.2558 - acc: 0.9298 - val_loss: 0.3311 - val_acc: 0.8997
Learning rate:  5e-07
Epoch 120/120
134/133 [==============================] - 28s 212ms/step - loss: 0.2578 - acc: 0.9305 - val_loss: 0.3326 - val_acc: 0.8978
Out[12]:
<tensorflow.python.keras.callbacks.History at 0x1bd8fd7e898>
In [13]:
## Now the training is complete, we get
# another object to load the weights
# compile it, so that we can do 
# final evaluation on it
modelGo.load_weights(filepath)
modelGo.compile(loss='categorical_crossentropy', 
                optimizer='adam', 
                metrics=['accuracy'])
In [14]:
# Make classification on the test dataset
predicts    = modelGo.predict(tsDat)

# Prepare the classification output
# for the classification report
predout     = np.argmax(predicts,axis=1)
testout     = np.argmax(tsLbl,axis=1)
labelname   = ['flower', 'non-flower']
                                            # the labels for the classfication report


testScores  = metrics.accuracy_score(testout,predout)
confusion   = metrics.confusion_matrix(testout,predout)


print("Best accuracy (on testing dataset): %.2f%%" % (testScores*100))
print(metrics.classification_report(testout,predout,target_names=labelname,digits=4))
print(confusion)
Best accuracy (on testing dataset): 90.72%
              precision    recall  f1-score   support

      flower     0.8850    0.8987    0.8918       454
  non-flower     0.9241    0.9135    0.9188       613

    accuracy                         0.9072      1067
   macro avg     0.9046    0.9061    0.9053      1067
weighted avg     0.9075    0.9072    0.9073      1067

[[408  46]
 [ 53 560]]
In [15]:
import pandas as pd

records     = pd.read_csv(modelname +'.csv')
plt.figure()
plt.subplot(211)
plt.plot(records['val_loss'])
plt.plot(records['loss'])
plt.yticks([0, 0.20, 0.30, 0.4, 0.5])
plt.title('Loss value',fontsize=12)

ax          = plt.gca()
ax.set_xticklabels([])



plt.subplot(212)
plt.plot(records['val_acc'])
plt.plot(records['acc'])
plt.yticks([0.7, 0.8, 0.9, 1.0])
plt.title('Accuracy',fontsize=12)
plt.show()
In [16]:
wrong_ans_index = []

for i in range(len(predout)):
    if predout[i] != testout[i]:
        wrong_ans_index.append(i)
In [17]:
wrong_ans_index = list(set(wrong_ans_index))
In [18]:
# Randomly show X examples of that was wrong

dataset = tsDatOrg #flowers #fungus #rocks

for index in wrong_ans_index:
    #index = wrong_ans_index[random.randint(0, len(wrong_ans_index)-1)]
    print("Showing %s index image" %(index))
    print("Predicted as %s but is actually %s" %(predout[index], testout[index]))
    imgplot = plt.imshow(data[index])
    plt.show()
Showing 522 index image
Predicted as 0 but is actually 1
Showing 1034 index image
Predicted as 1 but is actually 0
Showing 12 index image
Predicted as 0 but is actually 1
Showing 1044 index image
Predicted as 1 but is actually 0
Showing 1045 index image
Predicted as 1 but is actually 0
Showing 1047 index image
Predicted as 1 but is actually 0
Showing 26 index image
Predicted as 0 but is actually 1
Showing 539 index image
Predicted as 0 but is actually 1
Showing 540 index image
Predicted as 0 but is actually 1
Showing 1054 index image
Predicted as 1 but is actually 0
Showing 1056 index image
Predicted as 1 but is actually 0
Showing 545 index image
Predicted as 0 but is actually 1
Showing 37 index image
Predicted as 0 but is actually 1
Showing 557 index image
Predicted as 0 but is actually 1
Showing 596 index image
Predicted as 0 but is actually 1
Showing 89 index image
Predicted as 0 but is actually 1
Showing 92 index image
Predicted as 0 but is actually 1
Showing 605 index image
Predicted as 0 but is actually 1
Showing 615 index image
Predicted as 1 but is actually 0
Showing 616 index image
Predicted as 1 but is actually 0
Showing 622 index image
Predicted as 1 but is actually 0
Showing 113 index image
Predicted as 0 but is actually 1
Showing 131 index image
Predicted as 0 but is actually 1
Showing 645 index image
Predicted as 1 but is actually 0
Showing 650 index image
Predicted as 1 but is actually 0
Showing 148 index image
Predicted as 0 but is actually 1
Showing 662 index image
Predicted as 1 but is actually 0
Showing 164 index image
Predicted as 0 but is actually 1
Showing 169 index image
Predicted as 0 but is actually 1
Showing 172 index image
Predicted as 0 but is actually 1
Showing 176 index image
Predicted as 0 but is actually 1
Showing 695 index image
Predicted as 1 but is actually 0
Showing 204 index image
Predicted as 0 but is actually 1
Showing 725 index image
Predicted as 1 but is actually 0
Showing 726 index image
Predicted as 1 but is actually 0
Showing 219 index image
Predicted as 0 but is actually 1
Showing 220 index image
Predicted as 0 but is actually 1
Showing 733 index image
Predicted as 1 but is actually 0
Showing 228 index image
Predicted as 0 but is actually 1
Showing 743 index image
Predicted as 1 but is actually 0
Showing 233 index image
Predicted as 0 but is actually 1
Showing 250 index image
Predicted as 0 but is actually 1
Showing 253 index image
Predicted as 0 but is actually 1
Showing 774 index image
Predicted as 1 but is actually 0
Showing 775 index image
Predicted as 1 but is actually 0
Showing 269 index image
Predicted as 0 but is actually 1
Showing 790 index image
Predicted as 1 but is actually 0
Showing 279 index image
Predicted as 0 but is actually 1
Showing 792 index image
Predicted as 1 but is actually 0
Showing 794 index image
Predicted as 1 but is actually 0
Showing 796 index image
Predicted as 1 but is actually 0
Showing 288 index image
Predicted as 0 but is actually 1
Showing 805 index image
Predicted as 1 but is actually 0
Showing 821 index image
Predicted as 1 but is actually 0
Showing 311 index image
Predicted as 0 but is actually 1
Showing 830 index image
Predicted as 1 but is actually 0
Showing 329 index image
Predicted as 0 but is actually 1
Showing 336 index image
Predicted as 0 but is actually 1
Showing 347 index image
Predicted as 0 but is actually 1
Showing 349 index image
Predicted as 0 but is actually 1
Showing 355 index image
Predicted as 0 but is actually 1
Showing 871 index image
Predicted as 1 but is actually 0
Showing 365 index image
Predicted as 0 but is actually 1
Showing 366 index image
Predicted as 0 but is actually 1
Showing 367 index image
Predicted as 0 but is actually 1
Showing 880 index image
Predicted as 1 but is actually 0
Showing 371 index image
Predicted as 0 but is actually 1
Showing 375 index image
Predicted as 0 but is actually 1
Showing 889 index image
Predicted as 1 but is actually 0
Showing 893 index image
Predicted as 1 but is actually 0
Showing 382 index image
Predicted as 0 but is actually 1
Showing 384 index image
Predicted as 0 but is actually 1
Showing 904 index image
Predicted as 1 but is actually 0
Showing 398 index image
Predicted as 0 but is actually 1
Showing 914 index image
Predicted as 1 but is actually 0
Showing 918 index image
Predicted as 1 but is actually 0
Showing 919 index image
Predicted as 1 but is actually 0
Showing 420 index image
Predicted as 0 but is actually 1
Showing 421 index image
Predicted as 0 but is actually 1
Showing 938 index image
Predicted as 1 but is actually 0
Showing 942 index image
Predicted as 1 but is actually 0
Showing 947 index image
Predicted as 1 but is actually 0
Showing 950 index image
Predicted as 1 but is actually 0
Showing 955 index image
Predicted as 1 but is actually 0
Showing 964 index image
Predicted as 1 but is actually 0
Showing 974 index image
Predicted as 1 but is actually 0
Showing 975 index image
Predicted as 1 but is actually 0
Showing 469 index image
Predicted as 0 but is actually 1
Showing 470 index image
Predicted as 0 but is actually 1
Showing 471 index image
Predicted as 0 but is actually 1
Showing 982 index image
Predicted as 1 but is actually 0
Showing 474 index image
Predicted as 0 but is actually 1
Showing 994 index image
Predicted as 1 but is actually 0
Showing 487 index image
Predicted as 0 but is actually 1
Showing 491 index image
Predicted as 0 but is actually 1
Showing 498 index image
Predicted as 0 but is actually 1
Showing 1015 index image
Predicted as 1 but is actually 0
Showing 505 index image
Predicted as 0 but is actually 1
Showing 1022 index image
Predicted as 1 but is actually 0
In [ ]: